package com.vanke.kotlin_mvp.taskdetail

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.vanke.kotlin_mvp.R
import com.vanke.kotlin_mvp.data.Injection
import com.vanke.kotlin_mvp.util.ActivityUtils
import kotlinx.android.synthetic.main.activity_task_detail.*

class TaskDetailActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_task_detail)

        setSupportActionBar(toolbar)

        supportActionBar?.let {
            it.setDisplayHomeAsUpEnabled(true)
            it.setDisplayShowHomeEnabled(true)
        }

        val taskId = intent.getStringExtra(EXTRA_TASK_ID)

        val taskDetailFragment = supportFragmentManager.findFragmentById(R.id.contentFrame) as TaskDetailFragment? ?:
                TaskDetailFragment.newInstance(taskId).also {
                    ActivityUtils.addFragmentToActivity(supportFragmentManager, it, R.id.contentFrame)
                }

        TaskDetailPresenter(taskId,Injection.provideTasksRepository(applicationContext),taskDetailFragment)
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }

    companion object {
        val EXTRA_TASK_ID ="TASK_ID"
    }
}
